//
//  BaseViewController.m
//  CitizenCard
//
//  Created by 何焯荣 on 17/5/31.
//  Copyright © 2017年 nationz. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@property (nonatomic, strong) NSArray *style2Array;

@end

@implementation BaseViewController

//灰色背景，白色状态栏
- (NSArray *)style2Array {
    if (!_style2Array) {
        _style2Array = @[
                         @"LifeViewController",
                         @"MineViewController",
                         @"HaveNetIDVC",
                         @"WalletVC",
                         @"CCStarryQRCode",
                         @"CardStoreListVC",
                         ];
    }
    return _style2Array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] init];
    backButtonItem.title = @"";
    self.navigationItem.backBarButtonItem = backButtonItem;

    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([self.style2Array containsObject:NSStringFromClass([self class])]) {
        UIView * barBackground = self.navigationController.navigationBar.subviews.firstObject;
        if (@available(iOS 11.0, *))
        {
            barBackground.alpha = 0;
            [barBackground.subviews setValue:@(0) forKeyPath:@"alpha"];
        } else {
            barBackground.alpha = 0;
        }
        [self findHairlineImageViewUnder:self.navigationController.navigationBar].hidden = YES;
    }
    [self setupNaviStyle];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if ([self.style2Array containsObject:NSStringFromClass([self class])]) {
        UIView * barBackground = self.navigationController.navigationBar.subviews.firstObject;
        if (@available(iOS 11.0, *))
        {
            barBackground.alpha = 1;
            [barBackground.subviews setValue:@(1) forKeyPath:@"alpha"];
        } else {
            barBackground.alpha = 1;
        }
        [self findHairlineImageViewUnder:self.navigationController.navigationBar].hidden = NO;
    }
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupNaviStyle {
    
    if ([self.style2Array containsObject:NSStringFromClass([self class])]) {
        [self setStyle2];
    } else {
        [self setStyle1];
    }
}

- (void)setStyle1 {
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:UIColorWithRGB(0x33, 0x33, 0x33),NSFontAttributeName:[UIFont systemFontOfSize:18]};
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.tintColor  = UIColorWithRGB(0x33, 0x33, 0x33);
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)setStyle2 {
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]};
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.tintColor  = [UIColor whiteColor];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)dealloc{
    NSLog(@"%@ dealloc", NSStringFromClass([self class]));
}
@end
